<?php
require './site/dkp/dkpfunctions.php';

if (isEditable())
{
	if (isset($_GET['setHidden']))
		setHidden($_GET['setHidden']);
	if (isset($_POST['setBonus']))
		setBonus($_POST['setBonus']);
	if (isset($_POST['wwsURL']))
		setWWSURL($_POST['wwsURL']);
	if (isset($_POST['addRaid']))
		addRaid();
	if (isset($_POST['addMemberToRaid']))
		addMemberToRaid();
	if (isset($_POST['removeMemberFromRaid']))
		removeMemberFromRaid();
	if (isset($_POST['addItemToRaid']))
		addItemToRaid();
	if (isset($_POST['removeItemFromRaid']))
		removeItemFromRaid();
}
showPageHeader();
showIntroText();
if (isEditable() && isset($_GET['action']) && $_GET['action'] == 'new') {
	displayCreateRaidForm();
} else {
	showDetail();
}
showPageFoot();

// -------------------------------------------------------------
//  ----  Functions  -----------------------------------------
// -------------------------------------------------------------
function showPageHeader()
/*
Opens the pages middle section content box and adds the title
*/
{
?>
    	<div id="content">
			<?php showDKPTitlebar('DKP Raids - Beta'); ?>
            <div class="box">
                <div class="contents">
<?php
}
// -------------------------------------------------------------
function showPageFoot()
/*
Displays final links and closes the pages middle section content box
*/
{
?>
                </div>
            </div>
			<?php 
				if (isEditable() && isset($_GET['action']) && $_GET['action'] == 'update')
					displayDKPLog(20); 
			?>
    	</div>
<?php
}
// -------------------------------------------------------------
function manageLink()
/*
Displays the update page link for those authorised to use it
*/
{
	if (isEditable()) {
		if (isset($_GET['action']) && $_GET['action'] == 'update')
		{
			echo '<img src="./images/common/icons/stop.png" style="vertical-align:middle;"/>&nbsp;<a href="index.php?page=dkpraids" style="vertical-align:middle;">Exit update page</a><br />';
		} else {
			echo '<a href="index.php?page=dkpraids&action=update">Update page</a><br />';
		}
	}
}
// -------------------------------------------------------------
function showIntroText()
/*
Displays the text at the top of the page
*/
{
	?>
	<div style="background-image:url(./site/dkp/images/wh-power.png); background-repeat:no-repeat; background-position:right; padding: 1px 1px 8px 1px;margin: 2px 0px 2px 2px; text-align: left;">
		<p style="padding: 5px 10px 10px 4px;">
			Below are the DMZ raids.
			<?php 
				if (isEditable()) {
					if ($_GET['action'] == 'new' || $_GET['action'] == 'update') {
						echo '<br /><img src="./images/common/icons/stop.png" style="vertical-align:middle;"/>&nbsp;<a href="index.php?page=dkpraids" style="vertical-align:middle;">Exit update page</a><br />'; 
					} else {
						echo '<br />'; showNewRaidLink();
					}
				}
			?>
		</p>
	</div>
	<?php
}
// -------------------------------------------------------------
function showDetail()
{
	$raidArray = getAllRaids();
	if (isset($_GET['r'])) {
		$raidID = $_GET['r'];
	} else {
		$raidID = getLastRaidID($raidArray);
	}
	if (isEditable() && isset($_GET['action']) && $_GET['action'] == 'update') {
		if (isset($_GET['r'])) {
			showRaidDetail($raidID);
		} else {
			showNewRaidLink(); 
			echo '<br /><br />';
			showRaidList($raidArray, $raidID);
		}
	} else {
		showRaidDetail($raidID);
		showRaidList($raidArray, $raidID);
	}
}
// -------------------------------------------------------------
function showRaidDetail($raidID)
{
	$raidDetailArray = getRaidDetails($raidID);
	$raidItemsArray = getRaidItems($raidID);
	$addedRaiders[] = 0;
// Raid title
	echo '<img src="./site/dkp/images/instances/'.$raidDetailArray[0][DungeonID].'.gif"/>&nbsp;';
	echo '<span style="font-weight: bold; font-size: 18px;">'.$raidDetailArray[0][DungeonName].'</span>&nbsp;-&nbsp;';
	echo date('l j F \'y', strtotime($raidDetailArray[0][Date]));
	echo '<br /><br />';
// Show/Hide toggler <<edit mode>>
	if (isEditable() && isset($_GET['action']) && $_GET['action'] == 'update' && isset($_GET['r'])) {
		if ($raidDetailArray[0][hidden] == 0) {
			echo '<table cellspacing="0"><tr><td style="padding: 3px 6px 3px 6px; border: 1px solid #666" width="120px">This raid is visible</td><td style="background-color: #e49d9d; padding: 3px 6px 3px 6px; border: 1px solid #666" width="50px"><a href="http://dmz-gaming.com/index.php?page=dkpraids&action=update&r='.$raidID.'&setHidden=1">Hide</a></td><td></td></tr></table>';
		} else {
			echo '<table cellspacing="0"><tr><td style="background-color: #e49d9d; padding: 3px 6px 3px 6px; border: 1px solid #666" width="120px">This raid is hidden</td><td style="padding: 3px 6px 3px 6px; border: 1px solid #666" width="50px"><a href="http://dmz-gaming.com/index.php?page=dkpraids&action=update&r='.$raidID.'&setHidden=0">Show</a></td><td></td></tr></table>';
		}
		$signupArray = getSignedMembers();
	}

	if (isEditable() && isset($_GET['action']) && $_GET['action'] == 'update' && isset($_GET['r']))
		echo '<form method="post" action="index.php?page=dkpraids&action=update&r='.$_GET['r'].'" name="removeMemberFromRaid">';
	?>
		<br />
		<table>
			<tr>
				<td valign="top">
					<?php
					$count = 0; $currRole = 0;
					foreach ($raidDetailArray as $raider)
					{
						if ((time() - strtotime($raider[Date])) > getBunusExpiration()) { //expired
							$expiredtext = ' (expired)';
							$expiredColour = ' style="color:#c00"';
						}
						if ($raider[RoleID] == $currRole)
							{/*Do nothing*/} else {$currRole = $raider[RoleID]; $count++;}
						if ($count == 2)
							{echo '<br />'; $count++;}
						if ($count == 4 || $count == 6)
							{echo '</td><td valign="top">'; $count++;}
						$raiderName = $raider[MemberName];
						$titleText= '';
						if (isEditable() && isset($_GET['action']) && $_GET['action'] == 'update' && isset($_GET['r'])) {
							$addedRaiders[] = $raider[MemberID];
							$addedRaidersnames[] = $raider[MemberName];
							echo '<input type="checkbox" name="removeMemberFromRaid[]" value="'.$raider[MemberID].'">&nbsp;';
							if (strlen($raiderName) > 9)
								$raiderName = substr($raiderName, 0, 7).'...';
							$previousRaidArray = getNumPreviousRaids($_GET['r']);
							$titleText= 'Raids attended: '.$previousRaidArray[$raider[MemberName]];
							$fontWeight = 'normal';
							if (isEditable() && isset($_GET['action']) && $_GET['action'] == 'update') {
								foreach ($signupArray as $signee) {
									$pos = strpos(strtolower(html_entity_decode($signee)), strtolower($raider[MemberName]));
									if ($pos === false) {
										// do nothing
									} else {
										$fontWeight = 'bold';
									}
								}
							}
						}
						displayRaider($raider[MemberID], $raider[RoleImage], $raider[RoleName], $raider[ClassImage], $raider[ClassName], $raider[ClassColour], $raiderName, '<br />', $titleText, $fontWeight);
					}
					if (isEditable() && isset($_GET['action']) && $_GET['action'] == 'update' && isset($_GET['r'])) {
						$numAddedRaiders = count($addedRaiders) - 1;
						echo '<p style="text-align: left"><input type="submit" value="Remove raider(s)"><br />Total: '.$numAddedRaiders.'</p></form>';
					}
					?>
				</td>
			</tr>
		</table>
		<br />
		<table>
			<tr>
				<td width="50%"><span style="font-weight: bold; font-size: 14px;">Raid Drops:</span></td>
				<td><img src="./images/wow_icons/Spell_ChargePositive.png" title="Bonus for this raid<?php echo $expiredtext; ?>" height="16px" width="16px"/><span<?php echo $expiredColour; ?>> <?php echo $raider[BonusValue]; ?> points</span></td>
				<td>
					<div style="text-align: right;">
						<?php
						if ($raider[wws_url] != NULL)
							echo '<a href="'.$raider[wws_url].'" title="View the stats for this raid." target="_BLANK" style="vertical-align:middle;">Stats report</a>&nbsp;<img src="./images/common/icons/chart_curve.png" style="vertical-align:middle;"/>';
						?>
					</div>
				</td>
			</tr>
		</table>
		<div>
			<?php showRaidLoot($raidID); ?>
		</div>
		<br />
	<?php
	if (isEditable() && isset($_GET['action']) && $_GET['action'] == 'update') {
		echo '
		<div id="accordion">
			<a href="#raidBuffs" class="toggler"><b>Raid Buffs</b></a><br />
			<div class="element">
		';
		echo getRaidBuffs();
		echo '
			</div>
		</div>
		';
		echo getInviteList($addedRaidersnames);
		showEditRaidDetail($raidDetailArray, $addedRaiders, $signupArray);
	}
}
// -------------------------------------------------------------
function getInviteList($addedRaidersnames) 
{
	$inviteList =  '
		<div id="accordion">
			<a href="#invites" class="toggler"><b>Invites</b></a><br />
			<div class="element">
			';
		foreach ($addedRaidersnames as $invName)
		{
			$invites .= '/i '.$invName.'<br />';
		}
	$inviteList .= $invites;
	$inviteList .= '
			</div>
		</div>
	';
	return $inviteList;
}
// -------------------------------------------------------------

/* -------------------------------------------------------------
 -------------------------------------------------------------*/

?>
